﻿Imports MeoTom.Core

Partial Public Class ChangePassword
    Inherits UI.Page

    Private ReadOnly _u As New BLL.Users()
    Private ReadOnly _util As New Utilities.Common()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim previousPage As String = ""
        If Request.QueryString.ToString = vbNullString Then
            previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO"))
        Else
            previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO") & "?" & Request.QueryString.ToString())
        End If
        _util.ValidateLogin(previousPage)
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
        lblError.Attributes.Remove("style")
        If txtOldPass.Text = vbNullString Then
            lblError.InnerHtml = "* Old Password Require !!"
            txtOldPass.Focus()
        ElseIf txtNewPass.Text = vbNullString Then
            lblError.InnerHtml = "* New Password Require !!"
            txtNewPass.Focus()
        ElseIf txtNewPass.Text.Length < 7 Then
            lblError.InnerHtml = "* New password should be at least seven characters long !!"
            txtNewPass.Focus()
        ElseIf Not String.Equals(txtNewPass.Text, txtReNewPass.Text) Then
            lblError.InnerHtml = "* ReType Password does not match !!"
            txtReNewPass.Focus()
        Else
            Dim strUserID As String = ""
            Dim strUserName As String = ""
            Dim strNewPass As String = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtNewPass.Text, "MD5")
            Dim strOldPass As String = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtOldPass.Text, "MD5")
            If _util.GetCookie("UserID") = vbNullString Then
                strUserID = _util.GetSession("UserID")
                strUserName = _util.GetSession("UserName")
            Else
                strUserID = _util.GetCookie("UserID")
                strUserName = _util.GetCookie("UserName")
            End If


            _u.UserName = strUserName
            _u.Password = strOldPass

            If _u.Login() Then
                _u.ID = Integer.Parse(strUserID)
                _u.Password = strNewPass
                _u.ResetPassword()
                lblError.InnerHtml = "* Your Password has been changed !!"
            Else
                lblError.InnerHtml = "* Incorrect Old Password !!"
            End If
        End If
    End Sub
End Class